package com.example.tuike.mapper;
import com.example.tuike.common.utils.ContentsSqlProvider;
import com.example.tuike.entity.Contents;
import org.apache.ibatis.annotations.*;
import java.util.List;
/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/2/2 21:09

 * @Version 1.0*/


@Mapper
public interface ContentsMapper {

    @Select("SELECT * FROM Contents")
    List<Contents> findAll();

    @Select("SELECT * FROM Contents WHERE contentId = #{contentId}")
    Contents findById(@Param("contentId") Long contentId);

    @Insert("INSERT INTO Contents (feedId, title, link, content, hash, publishedAt) VALUES (#{feedId}, #{title}, #{link}, #{content}, #{hash}, #{publishedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "contentId")
    void insert(Contents content);

    @Update("UPDATE Contents SET feedId = #{feedsId}, title = #{title}, link = #{link}, content = #{content}, hash = #{hash}, publishedAt = #{publishedAt} WHERE contentId = #{contentId}")
    void update(Contents content);

    @Delete("DELETE FROM Contents WHERE contentId = #{contentId}")
    void delete(@Param("contentId") Long contentId);

    // 如果需要检查链接和哈希值的组合是否唯一
    @Select("SELECT COUNT(*) FROM Contents WHERE link = #{link} AND hash = #{hash}")
    int countByPermalinkAndHash(@Param("link") String link, @Param("hash") String hash);

    @Select("SELECT * FROM Contents WHERE feedId=#{feedId}")
    List<Contents>findByFeedId(@Param("feedId")Integer feedId);

    //普通分页
    @SelectProvider(type = ContentsSqlProvider.class, method = "selectContentsByUserIdAndFeedId")
    List<Contents> findContentsByFeedId(
            @Param("feedId") Integer feedId,
            @Param("pageNumber") int pageNumber,
            @Param("offset") int offset,
            @Param("pageSize") int pageSize);
    //游标分页
    @SelectProvider(type = ContentsSqlProvider.class, method = "selectContentsByFeedIdWithCursor")
    List<Contents> getContentsByFeedIdWithCursor(
            @Param("feedId") Integer feedId,
            @Param("cursor") Long cursor,
            @Param("limit") int limit);
}